/*
 * Copyright (c) 2006, 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx.serviet;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>Project: javaweb - Timers
 * <p>Powered by webrx On 2022-03-14 14:57:24
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
@WebServlet("/save")
public class Save extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
          int id = Integer.parseInt(req.getParameter("id").toString());
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("name");


        System.out.println(id);
        System.out.println(name);

        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();

        DruidDataSource ds = new DruidDataSource();
        ds.setUrl("jdbc:mysql:/db");
        ds.setUsername("root");

        QueryRunner run = new QueryRunner(ds);
        List<String> dbs = new ArrayList<>();
        try{
            dbs = run.query("show databases",new ColumnListHandler<>());
        }catch(Exception e){
            e.printStackTrace();
        }
        StringBuilder sbu = new StringBuilder();
        sbu.append("[".concat(dbs.stream().map(e->"\""+e+"\"").collect(Collectors.joining(","))).concat("]"));
        out.print(sbu.toString());
        out.flush();
        out.close();
    }
}
